package com.healthybear.demo.languagechange

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment

/**
 * ContentFragment displays localized content for ViewPager2 pages
 * 
 * Requirements addressed:
 * - 5.4: Update fragment content when language is changed
 * - 5.5: Maintain selected language when navigating between ViewPager2 pages
 */
class ContentFragment : Fragment() {
    
    private var pageNumber: Int = 0
    
    companion object {
        private const val ARG_PAGE_NUMBER = "page_number"
        
        /**
         * Factory method to create ContentFragment instances
         * 
         * @param pageNumber The page number for this fragment (0-based)
         * @return New ContentFragment instance
         */
        fun newInstance(pageNumber: Int): ContentFragment {
            return ContentFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PAGE_NUMBER, pageNumber)
                }
            }
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        pageNumber = arguments?.getInt(ARG_PAGE_NUMBER) ?: 0
    }
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_content, container, false)
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupContent(view)
    }
    
    /**
     * Set up the content for this page based on the page number
     * Different content for each page to demonstrate language switching
     */
    private fun setupContent(view: View) {
        val titleTextView = view.findViewById<TextView>(R.id.contentTitle)
        val descriptionTextView = view.findViewById<TextView>(R.id.contentDescription)
        
        when (pageNumber) {
            0 -> {
                titleTextView.text = getString(R.string.page_title_1)
                descriptionTextView.text = getString(R.string.page_content_1) + "\n\n" + getString(R.string.page_description_1)
            }
            1 -> {
                titleTextView.text = getString(R.string.page_title_2)
                descriptionTextView.text = getString(R.string.page_content_2) + "\n\n" + getString(R.string.page_description_2)
            }
            2 -> {
                titleTextView.text = getString(R.string.page_title_3)
                descriptionTextView.text = getString(R.string.page_content_3) + "\n\n" + getString(R.string.page_description_3)
            }
            else -> {
                titleTextView.text = getString(R.string.page_title_1)
                descriptionTextView.text = getString(R.string.page_content_1) + "\n\n" + getString(R.string.page_description_1)
            }
        }
    }
}